#include <malloc.h>
#include <stdio.h>




int main (int argc, char* argv[])
{
	printf("argc = %d\n", argc);
	
	if(argc <= 2)
	{	
		printf("usage:\n");
		printf("program_name <document's pages> <number of page for sheet> \n");
		return 1;
	}	
	
	int * pari, * dispari;
	int tmp, i, j, r, k, size=1;

	int pag_tot=atoi(argv[1]);
	int pag_x_pag=atoi(argv[2]);


	if ( pag_tot % 2 )	
	{
		tmp= (pag_tot +1) /2 ;
		r=tmp%pag_x_pag;
                if (r)
                        size=tmp+(pag_x_pag-r);
		else
			size=tmp;	
	}
	else
	{
	 	tmp= pag_tot /2 ;
		r=tmp%pag_x_pag;
		if (r)
                        size=tmp+(pag_x_pag-r);
                else
                        size=tmp;

	}

	printf("debug size: %d\n", size);

	pari 	= (int*) malloc (size * sizeof(int));
	dispari = (int*) malloc (size * sizeof(int));

	for(i=0;i<size;i++)
	{
		dispari[i]=pari[i]=0;
	}
	
	for(i=1,j=0,k=0;i<=pag_tot;i++)
	{
		if( ( (i-1)/pag_x_pag) %2 ==0 )//pagina pari
			pari[j++]=i;
		else
			dispari[k++]=i;
			
		

	}

	printf("\n front: ");
	for(i=0;i<size;i++)
        {
              printf("%d,", pari[i]);
        }

	printf("\n back:  ");
        for(i=0;i<size;i++)
        {
              printf("%d,", dispari[i]);
        }
	printf("\n");


	return 0;
}

